from app.config import SessionLocal, engine
from fastapi import Form, Request, APIRouter, Depends
from sqlalchemy.orm import Session
from . import crud, schema
from ..auth import security

transaction_router = APIRouter(prefix="/api/transactions")

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@transaction_router.get("/categories/all")
def get_all_categories(db: Session = Depends(get_db)):
    return crud.get_categories(db)

@transaction_router.post("/categories/add")
def add_category(model: schema.CategotyCreate, db: Session = Depends(get_db)):
    return crud.add_category(db, model.name)

@transaction_router.post("/add")
def add_transcation(model: schema.TranscationCreate, db: Session = Depends(get_db)):
    return crud.add_transcations(db, int(security.get_user_id_by_token(model.token)),
     model.name, model.cost, model.category_id)

@transaction_router.post("/get_all_transactions")
def get_all_transactions(model: schema.Auth, db: Session = Depends(get_db)):
    return crud.get_transactions(db, security.get_user_id_by_token(model.token))